#include "frm_routine_menu.h"
#include "ui_frm_routine_menu.h"
#include "frm_exercises.h"
#include "frm_menu.h"
#include "frm_beats_aca_result.h"
#include "frm_delete.h"
#include "frm_exercises_current.h"
#include <QDesktopWidget>
#include <QtCore/QFile>
#include <QtCore/QTextStream>
#include <QDate>
#include <QString>
#include <QFile>
#include <QTextStream>
#include <QString>
#include <QMessageBox>

frm_routine_menu::frm_routine_menu(QDialog *parent) :
    QDialog(parent),
    ui(new Ui::frm_routine_menu)
{
    ui->setupUi(this);
    setWindowFlags( Qt:: WindowTitleHint | Qt:: CustomizeWindowHint | Qt:: WindowMinimizeButtonHint | Qt:: WindowMaximizeButtonHint);
    this->move(QApplication::desktop()->screen()->rect().center() - this->rect().center());


   //Comprobamos que no exista una rutina creada.


   if (comprobar()==1)
   {
       ui->btnChange->setEnabled(true);
       ui->btnDelete->setEnabled(true);
       ui->btnStart->setEnabled(true);
       ui->btnNew->setEnabled(false);
   }
   else
   {
       ui->btnChange->setEnabled(false);
       ui->btnDelete->setEnabled(false);
       ui->btnStart->setEnabled(false);
       ui->btnNew->setEnabled(true);

   }

}

frm_routine_menu::~frm_routine_menu()
{
    delete ui;
}

void frm_routine_menu::on_btnNew_clicked()
{
    frm_exercises *exercises = new frm_exercises();
    this->hide();
    exercises->show();
}

void frm_routine_menu::on_btnBack_clicked()
{
    frm_menu *menu = new frm_menu();
    this->hide();
    menu->show();
}


void frm_routine_menu::on_btnDelete_clicked()
{
    frm_delete *del = new frm_delete();
    this->hide();
    if (del->exec())
    {
        QFile Monday("exMonday.txt");
        QFile Tuesday("exTuesday.txt");
        QFile Wednesday("exWednesday.txt");
        QFile Thursday("exThursday.txt");
        QFile Friday("exFriday.txt");
        QFile Saturday("exSaturday.txt");
        QFile Sunday("exSunday.txt");

        Monday.open(QIODevice::WriteOnly);
        Tuesday.open(QIODevice::WriteOnly);
        Wednesday.open(QIODevice::WriteOnly);
        Thursday.open(QIODevice::WriteOnly);
        Friday.open(QIODevice::WriteOnly);
        Saturday.open(QIODevice::WriteOnly);
        Sunday.open(QIODevice::WriteOnly);

        Monday.close();
        Tuesday.close();
        Wednesday.close();
        Thursday.close();
        Friday.close();
        Saturday.close();
        Sunday.close();

        ui->btnChange->setEnabled(false);
        ui->btnDelete->setEnabled(false);
        ui->btnStart->setEnabled(false);
        ui->btnNew->setEnabled(true);
    }

    this->show();
 //   modificar(0);
}

void frm_routine_menu::on_btnChange_clicked()
{
    frm_exercises *ui = new frm_exercises();
    this->hide();
    ui->show();
}

//Precondicion:Comprueba si existe una rutina
//Postcondicion:Si el contenido del fichero routine.txt es 0 no existe si es 1 si existe.
bool comprobar()
{
    QString f;

    QFile file("routine.txt");

    if (file.open(QIODevice::ReadWrite| QIODevice::Text))
    {
    QTextStream in(&file);
    f = in.readLine();
    }
    else
    {
     qFatal("No puedo abrir el fichero para lectura.");
    }

    return (f.toInt());
}

void frm_routine_menu::on_btnStart_clicked()
{
    QDate *fecha = new QDate;
    QString ficherodia;
    if(fecha->currentDate().dayOfWeek() == 1)
        ficherodia="exMonday.txt";

    if(fecha->currentDate().dayOfWeek() == 2)
        ficherodia="exTuesday.txt";

    if(fecha->currentDate().dayOfWeek() == 3)
        ficherodia="exWednesday.txt";

    if(fecha->currentDate().dayOfWeek() == 4)
        ficherodia="exThursday.txt";

    if(fecha->currentDate().dayOfWeek() == 5)
        ficherodia="exFriday.txt";

    if(fecha->currentDate().dayOfWeek() == 6)
        ficherodia="exSaturday.txt";

    if(fecha->currentDate().dayOfWeek() == 7)
        ficherodia="exSunday.txt";

    QFile file(ficherodia);
    if(!file.open(QIODevice::ReadOnly))
        qFatal("Can't open exercises.txt");
    QTextStream in(&file);
    QString exercise;
    QStringList listaEjercicios;
    while(!in.atEnd()){
        exercise = in.readLine();
        listaEjercicios.push_back(exercise);
    }

    if (listaEjercicios.isEmpty())
    {
        QMessageBox mensaje;
        mensaje.setText("No exercises, sorry.\n");
        mensaje.exec();
    }else{


    QString exerfilename =listaEjercicios.front();
    listaEjercicios.pop_front();
    QFile exerfile(exerfilename+".txt");
    if(!exerfile.open(QIODevice::ReadOnly))
        qFatal("Can't open this exercise file");
    QTextStream inexercise(&exerfile);
    QVector<QString> data(4);
    QString dato;
    int i = 0;
    while (!inexercise.atEnd()){
        dato = inexercise.readLine();
        if (dato != "")
            if (i == 1 || i == 3)
                data[i]= dato;
            else
                data[i] = data[i] + "\n" + dato;
        else
            i++;
        }
    /*data[0] = inexercise.readLine();
    data[1] = inexercise.readLine();
    data[2] = inexercise.readLine();
    data[3] = inexercise.readLine();*/

    Frm_exercises_current *exer = new Frm_exercises_current(exerfilename,data[0],data[1],data[2],data[3],listaEjercicios);
    this->hide();
    exer->show();
}
}
